﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WCS.Controls.RunLog
{
    public partial class RunLog : UIPage
    {
        public RunLog()
        {
            runLog = this;
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
        }

        //在网站根目录下创建日志目录(bin文件夹→debug文件夹→logs文件夹)
        public static string path = AppDomain.CurrentDomain.BaseDirectory + "logs";

        //死锁
        public static object loglock = new object();

        public static RunLog runLog;

        /// <summary>
        /// 系统运行日志
        /// </summary>
        /// <param name="message"></param>
        public void Debug(string message)
        {
            uiRichTextBox1.AppendText($"{DateTime.Now} ：{message}\n");
            WriteLog("Debug", message);
        }

        /// <summary>
        /// 系统运行日志
        /// </summary>
        /// <param name="message"></param>
        public void Warning(string message)
        {
            uiRichTextBox1.SelectionColor = Color.Red;
            uiRichTextBox1.AppendText($"{DateTime.Now} ：{message}\n");
            WriteLog("Debug", message);
        }

        /// <summary>
        /// 异常日志
        /// </summary>
        /// <param name="message"></param>
        public void Error(string message)
        {
            uiRichTextBox1.SelectionColor = Color.Red;
            uiRichTextBox1.AppendText($"{DateTime.Now} ：{message}\n");
            WriteLog("Debug", message);
            WriteLog("Error", message);
        }

        public void InfoMES(string message)
        {
            uiRichTextBox1.SelectionColor = Color.Green;
            uiRichTextBox1.AppendText($"{DateTime.Now} ：{message}\n");
            WriteLog("InfoMES", message);
        }

        /// <summary>
        /// 打印日志
        /// </summary>
        /// <param name="type"></param>
        /// <param name="content"></param>
        protected static void WriteLog(string type, string content)
        {
            lock (loglock)
            {
                if (!Directory.Exists(path + "/" + type))//如果日志目录不存在就创建
                {
                    Directory.CreateDirectory(path + "/" + type);
                }

                string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");//获取当前系统时间
                string filename = path + "/" + type + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";//用日期对日志文件命名

                //创建或打开日志文件，向日志文件末尾追加记录
                StreamWriter mySw = File.AppendText(filename);

                //向日志文件写入内容
                string write_content = time + ": " + content;
                mySw.WriteLine(write_content);

                //关闭日志文件
                mySw.Close();
            }
        }

        private void 清除日志ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            uiRichTextBox1.Clear();
        }
    }
}

